<template>
  <div class="components-container">
    <div class="header-form">
        <div class="layui-inline">
          <label class="layui-form-label">逝者姓名</label>  
            <div class="layui-inline">
              <el-input  v-model="selectDataEixend.JpmNameEquals" placeholder="请输入逝者姓名"></el-input>
            </div>
        </div>
         <div class="layui-inline">
          <label class="layui-form-label">身份证号</label> 
            <div class="layui-inline">
              <el-input  v-model="selectDataEixend.JpmCardnoEquals" placeholder="请输入身份证号"></el-input>
            </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">业务编号</label>
            <div class="layui-inline">
              <el-input  v-model="selectDataEixend.JpmBussEquals" placeholder="请输入业务编号"></el-input>
            </div>
        </div>
         <div class="layui-inline">
        <label class="layui-form-label">开始日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectDataEixend.CremationdateGreaterAndEquals" type="date" placeholder="请选择日期">
          </el-date-picker>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">结束日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectDataEixend.CremationdateLessAndEquals" type="date" placeholder="请选择日期">
          </el-date-picker>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">最小年龄</label>
        <div class="layui-input-inline">
          <el-input  v-model="selectDataEixend.JpmAgeGreaterAndEquals" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" maxlength="3" placeholder="请输入最小年龄"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">最大年龄</label>
        <div class="layui-input-inline">
          <el-input  v-model="selectDataEixend.JpmAgeLessAndEquals" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" maxlength="3" placeholder="请输入最大年龄"></el-input>
        </div>
      </div>
      <div class="layui-inline" v-if="showdz">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-inline">
           <el-cascader  style="width:305px"  class="dz"  ref="address"  v-model="arr" :options="addressOptions" change-on-select>
           </el-cascader>    
        </div>
      </div>
          <el-button type="primary" @click="getData" :loading="this.$store.state.isloading">搜索</el-button>
          <el-button  @click="btnRepeat" >重置</el-button>
    </div>
    <div>
      <ul class="toolList">
        <li><el-button type="primary" @click="getData">刷新</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)" :key=index>
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable> -->
     <el-table border stripe :data="dataList" @sort-change="sortChange" highlight-current-row @current-change="selectRow" style="width: 100%; margin-bottom: 10px" fixed  :max-height="height" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" v-if="showselect"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click.native.prevent="operate('详情',scope.row)">
            详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column :prop="item.ColumnKey" :label="item.ColumnName" :order="item.ColumnOrder" :align="item.ColumnAlign" :width="item.ColumnWidth" :key="index" v-for="(item,index) in headList">   
        <el-table-column :prop="item.ColumnKey" :label="item.ColumnName" :order="item.ColumnOrder" :align="item.ColumnAlign" :width="item.ColumnWidth" :key="index" v-for="(item,index) in item.ColumnChild">   
        </el-table-column>
      </el-table-column>
    </el-table>
      <!-- 页码 -->
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageOrder.currentPage"
        :page-size="pageOrder.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageOrder.totalNum" >
      </el-pagination>
    </div>
    <informationRegistrationInfo ref="simpleReginfo" :orderType="order" @refreshData="getData"></informationRegistrationInfo>
    <!-- <cresimplereg ref="simpleReg" :orderType="order" @refreshData="getData"></cresimplereg> -->
    <!-- <printmodal ref="printPdf"></printmodal>
    <Appendixlists ref="dialog"></Appendixlists>
    <OperationHistory ref="OperationHistory"></OperationHistory>
    <informationRegistrationInfo ref="simpleReginfo" :orderType="order" @refreshData="getData"></informationRegistrationInfo>

     <sweet-modal ref="searchmessage" class="dialog" :sweetModalMaxWidth="pageWidth">
       <fieldset class="layui-elem-field layui-field-title">

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">处理日期</label>
              <div class="layui-input-inline backcolor">
                <el-date-picker v-date type="date" v-model="jpmData.JPBDATE"></el-date-picker>
              </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width:120px">是否需要联办</label>
                  <el-select v-model="jpmData.JPBISNEEDLB">
                  <el-option  label="是" value="1"> </el-option>
                  <el-option  label="否" value="0"> </el-option>
                </el-select>
            </div>
            </div> 
          <div class="layui-inline-item">
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline backcolor">
                  <el-input  type="textarea" class="longertexts"   :rows="2"  placeholder="备注"  v-model="jpmData.JPBDESC"></el-input>               
                </div>
            </div>
          </div>
     
          <div class="layui-form-item btnCenter">
            <el-button v-if="aa"  type="primary" :loading="this.$store.state.isloading" @click="Cancellationban">办结</el-button>
            <el-button   v-if="!aa" id="uploadss" type="primary" :loading="this.$store.state.isloading" @click="Cancellation">办结</el-button>
          </div>
        </fieldset> -->
  <!-- </sweet-modal> -->

  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-button + .el-button {
  margin-left: 0;
}
.longtext {
  width: 480px;
}
.longertext {
  width: 770px;
}
.sweet-btn {
  margin-top: 20px;
}
.radio {
  margin-top: 9px;
}
.longertexts {
  width: 535px;
}
</style>
<script>
import sweetModal from "@/components/sweetModal";
import dataTable from "@/components/Table/DataTable";
import apiBase from "@/api/apiBase.js";
import { ORDER } from "@/const/orderConst.js";
import { DATESELECTMODE } from "@/const/const.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import printmodal from "@/components/Pdf/Print";
import informationRegistrationInfo from "./informationRegistrationInfo";
import Appendixlists from "../jointPlatform/Appendixlists";
import OperationHistory from "./OperationHistory";
const IchValue = "D";
const UnIchValue = "E";
export default {
  name: "informationRegistration",
  mixins: [manage],
  data() {
    return {
      isUseNewAddress:null,
      showdz: null,
      arr: [],
      addressOptions: [],
      pageWidth: 1000,
      maxHeight: 800,
      showselect: false,
      currentRow: null,
      selectdata: [],
      DateSelectMode: DATESELECTMODE,
      IchValue: IchValue,
      UnIchValue: UnIchValue,
      AlldataList: [],

      selectDataEixend: {
        JpmNameEquals: null,
        JpmBussEquals: null,
        JpmCardnoEquals: null,
        IsHandle: true, //是否只查未处理数据
        CremationdateGreaterAndEquals: null,
        CremationdateLessAndEquals: null,
        JpmSaridLike: null //传输地址选择
      },
      jpmData: {
        JPBDATE: null,
        JPMCANCELUNIT: null,
        JPMCANCELUSR: null,
        JPBDESC: "", //办结备注
        JPBISNEEDLB: "0" //是否需要联办
      },
      aa: true,
    };
  },
  props: {
    height: {
      type: String,
      default: "480"
    }
  },
  methods: {
    // console.log(`每页 ${val} 条`);
    handleSizeChange(val) {
      this.pageOrder.pageSize = val;
      this.getData();
    },

    // console.log(`当前页: ${val}`);
    handleCurrentChange(val) {
      this.pageOrder.currentPage = val;
      this.getData();
    },
    selectRow(row) {
      this.currentRow = row;
    },
    dealData(id, option) {
      let level = "";
      for (let index = 0; index < option.length; index++) {
        if (id == option[index].value) {
          return option[index].level;
        } else if (option[index].children) {
          if (this.dealData(id, option[index].children)) {
            return this.dealData(id, option[index].children);
          }
        }
      }
    },
    getData() {
      let {
        JpmBussEquals,
        JpmNameEquals,
        CremationdateGreaterAndEquals,
        CremationdateLessAndEquals,
        JpmCardnoEquals,
        IsHandle
      } = this.selectDataEixend;
      let JpmSaridLike = this.arr[this.arr.length - 1];
      let SARID = this.arr[this.arr.length - 1];
      if(this.isUseNewAddress){
        JpmSaridLike = null
      }else{
        if (JpmSaridLike) {
          let level = this.dealData(JpmSaridLike, this.addressOptions);
          switch (level) {
            case "3":
              JpmSaridLike = JpmSaridLike.substring(0, 6);
              break;
            case "4":
              JpmSaridLike = JpmSaridLike.substring(0, 9);
              break;
            case "5":
              JpmSaridLike = JpmSaridLike.substring(0, 12);
              break;
            default:
              break;
          }
        }
        SARID = null
      }
      let datas = {
        SARID: SARID,
        JpmBussEquals: JpmBussEquals,
        JpmNameEquals: JpmNameEquals,
        JpmCardnoEquals: JpmCardnoEquals,
        IsHandle: IsHandle,
        JpmSaridLike: JpmSaridLike,
        CremationdateGreaterAndEquals: formatDate(
          CremationdateGreaterAndEquals,
          "yyyyMMdd"
        ),
        CremationdateLessAndEquals: formatDate(
          CremationdateLessAndEquals,
          "yyyyMMdd"
        ),
        JpmAgeGreaterAndEquals: this.selectDataEixend.JpmAgeGreaterAndEquals,
        JpmAgeLessAndEquals: this.selectDataEixend.JpmAgeLessAndEquals,
        DPTID: this.DeptId
      };
      let data = {
        PageIndex: this.pageOrder.currentPage,
        PageSize: this.pageOrder.pageSize,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: datas
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().JointRegConclude.QueryJpb,   
        res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
        }
      );
    },
    //获取表头
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetMyJoinConcludColumns,        
        res => {
          this.headList = res.data.content;
        }
      );
    },
    handleSelectionChange(val) {
      this.selectdata = val;
    },
    //排序
    sortChange(a, b, c) {
      for (var i in this.headList) {
        let item = this.headList[i];
        if (item.ColumnKey === a.prop) {
          this.pageOrder.orderType = item.ColumnOrder;
          break;
        }
      }
      if (a.order === "ascending") {
        this.pageOrder.orderSort = "TRUE";
        this.$emit("pageChange", this.pageOrder);
      } else {
        this.pageOrder.orderSort = "FALSE";
        this.$emit("pageChange", this.pageOrder);
      }
    },
    btnShow(btn) {
      if (btn === "详情" && this.currentRow) return true; //附件详情
      if (btn === "导出") return true; //导出
    },
    //功能操作
    operate(order, row) {
      this.order = order;
      // this.dataRepeat();
      switch (this.order) {
        case "详情":
          this.$refs.simpleReginfo.open("详情", row.JPMID);
          break;
        case "导出":
          this.export();
          break;
        default:
      }
    },
   // 导出
    async export(){
      await this.getalldata();
    //   console.log(this.AlldataList);

      if (this.AlldataList.length === 0 || this.AlldataList == null) {
        this.$message.error("无导出数据！");
        return;
      }
      var startTime;
      var endTime;  
      if (this.selectDataEixend.CremationdateGreaterAndEquals) {  //开始时间
        startTime = this.selectDataEixend.CremationdateGreaterAndEquals.toLocaleDateString();
      } else {
        startTime = "空";
      }
      if (this.selectDataEixend.CremationdateLessAndEquals) { //结束时间
        endTime = this.selectDataEixend.CremationdateLessAndEquals.toLocaleDateString();
      } else {
        endTime = "空";
      }

      let headername = "我的办结" + "(" + startTime + "-" + endTime + ")";
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(
          this.headList,
          this.AlldataList,
          headername
        );
      });
    },

    getalldata(){
      return new Promise((resolve,reject) => {
        let {
        JpmBussEquals,
        JpmNameEquals,
        CremationdateGreaterAndEquals,
        CremationdateLessAndEquals,
        JpmCardnoEquals,
        IsHandle
      } = this.selectDataEixend;
      let JpmSaridLike = this.arr[this.arr.length - 1];
      let SARID = this.arr[this.arr.length - 1];
      if(this.isUseNewAddress){
        JpmSaridLike = null
      }else{
        if (JpmSaridLike) {
          let level = this.dealData(JpmSaridLike, this.addressOptions);
          switch (level) {
            case "3":
              JpmSaridLike = JpmSaridLike.substring(0, 6);
              break;
            case "4":
              JpmSaridLike = JpmSaridLike.substring(0, 9);
              break;
            case "5":
              JpmSaridLike = JpmSaridLike.substring(0, 12);
              break;
            default:
              break;
          }
        }
        SARID = null
      }
      let datas = {
        SARID: SARID,
        JpmBussEquals: JpmBussEquals,
        JpmNameEquals: JpmNameEquals,
        JpmCardnoEquals: JpmCardnoEquals,
        IsHandle: IsHandle,
        JpmSaridLike: JpmSaridLike,
        CremationdateGreaterAndEquals: formatDate(
          CremationdateGreaterAndEquals,
          "yyyyMMdd"
        ),
        CremationdateLessAndEquals: formatDate(
          CremationdateLessAndEquals,
          "yyyyMMdd"
        ),
        JpmAgeGreaterAndEquals: this.selectDataEixend.JpmAgeGreaterAndEquals,
        JpmAgeLessAndEquals: this.selectDataEixend.JpmAgeLessAndEquals,
        DPTID: this.DeptId
      };
      let data = {
        PageIndex: -1,
        PageSize: 0,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: datas
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().JointRegConclude.QueryJpb,   
        res => {
          this.AlldataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
          resolve();
        }
      );
      })
    },
    //获取当前部门名称
    // getbmname() {
    //   let data = {};
    //   apiBase.DoService(
    //     data,
    //     apiBase.getApiAdress().JointRegistration.GetDpeName,
    //     res => {
    //       var s = res.data.content.DPTNAME;
    //       this.jpmData.JPMCANCELUNIT = s;
    //     }
    //   );
    // },
    //获取地址
    restructur(value) {
      let arr = JSON.parse(JSON.stringify(value));
      var data = [];
      arr.forEach(item => {
        if (item.Children.length > 0) {
          data.push({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL,
            children: this.restructur(item.Children)
          });
        } else {
          data.push({
            label: item.SARNAME,
            value: item.SARID,
            level: item.SARLEVEL
          });
        }
      });
      return data;
    },

    //查询条件重置
    btnRepeat() {
      this.selectDataEixend.JpmBussEquals = null;
      this.selectDataEixend.JpmNameEquals = null;
      this.selectDataEixend.JpmCardnoEquals = null;
      this.selectDataEixend.IsHandle = true;
      this.selectDataEixend.CremationdateGreaterAndEquals = null;
      this.selectDataEixend.CremationdateLessAndEquals = null;
      this.selectDataEixend.JpmSaridLike = null;
      this.arr = [];
      this.getData();
    },
    //数据重置
    dataRepeat() {
      // Object.assign(this.$data.regData, this.$options.data().regData);
      this.causList = [];
      this.ctyLists = [];
      // this.regDataTime = null;
      this.cauinfo = [];
      this.jpmData.JPBDESC = "";
      this.jpmData.JPBDATE = null;
      this.jpmData.JPBISNEEDLB = "0";
    }
  },
  mounted() {},
  components: {
    dataTable,
    sweetModal,
    informationRegistrationInfo,
    printmodal,
    Appendixlists,
    OperationHistory
  },
  activated() {
    this.getData();
    for (let btn of this.$store.state.btnListCommon) {
      if (btn.BTNVALUE === ORDER.REGISTER) {
        btn.BTNSHOWNAME = "修改";
        break;
      }
    }
    //控制地址下拉显隐
    apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsBySar, res => {
      let show = res.data.content;
      this.showdz = show;
    });
    // 获取级联选择
    apiBase.DoService({}, apiBase.getApiAdress().Code.GetBoroughByCode, res => {
      let data = res.data.content;
      this.addressOptions = data.map(item => ({
        label: item.SARNAME,
        value: item.SARID,
        level: item.SARLEVEL,
        children: item.Children.map(item => ({
          label: item.SARNAME,
          value: item.SARID,
          level: item.SARLEVEL,
          children: this.restructur(item.Children)
        }))
      }));
    });
  },
  created() {
    this.getColumns();
    this.DeptId = localStorage.getItem("DeptId");
    //是否启用新的联办地点查询（舟山联办）
    apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsUseNewAddress, res => {
      this.isUseNewAddress = res.data.content;
    });
  },
  watch: {}
};
</script>
